package com.oceanstar.controller;


import com.oceanstar.Pojo.*;
import com.oceanstar.service.IActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    /*
     *创建新的活动并发布到系统
     */
    @PostMapping("/activity/publish")
    public Result publish(@RequestBody ActivityInfo info, @RequestHeader("token") String token) {
        try {
            activityService.publishActivity(info,token);
            return Result.success();
        } catch (Exception e){
            return Result.error("发布失败: " + e.getMessage());
        }
    }

    /*
     *查询活动列表
     */
    @GetMapping("/activity/list")
    public Result getActivitiesList(@RequestParam("page") int page,
                                    @RequestParam("pageSize") int pageSize,
                                    @RequestParam("status") int status,
                                    @RequestHeader("token") String token) {
        try {
            PageInfo<ActivityDTO> pageInfo = activityService.getActivityList(page, pageSize,status);
            return Result.success(pageInfo);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }

    }

    /*
     *查看指定活动的报名人员清单。
     */
    @GetMapping("/activity/participants")
    public Result getParticipants(@RequestParam("activityId") int activityId,
                                  @RequestParam("page") int page,
                                  @RequestParam("pageSize") int pageSize,
                                  @RequestHeader("token") String token) {
        try {
            PageInfo<ActivityParticipantsDTO> pageInfo = activityService.getParticipantsList(activityId, page, pageSize);
            return Result.success(pageInfo);
        } catch (Exception e) {
            return Result.error("查询失败: " + e.getMessage());
        }
    }

    /*
     *为老人报名参加指定活动
     */
    @PostMapping("/activity/signup")
    public Result activitySignUp(@RequestBody ActivitySignUpInfo info, @RequestHeader("token") String token) {
        try {
            activityService.activitySignUp(info,token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("报名失败: " + e.getMessage());
        }
    }

    /*
     *取消已报名的活动。
     */
    @PostMapping("/activity/cancel")
    public Result activityCancel(@RequestBody ActivitySignUpInfo info, @RequestHeader("token") String token) {
        try {
            activityService.activityCancel(info,token);
            return Result.success();
        } catch (Exception e) {
            return Result.error("取消失败: " + e.getMessage());
        }
    }
}
